package com.eric.media.recorder;

import android.Manifest;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.eric.media.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;

@EActivity(R.layout.activity_recorder_sound)
public class RecorderSoundActivity extends AppCompatActivity {
    @ViewById
    Button imgBtn_pause;
    String path;
    private SoundRecordUtil soundRecordUtil;
    /**录制完成*/
    private  boolean isFinish = false;
    @AfterViews
    void initData(){
        soundRecordUtil = SoundRecordUtil.getInstance();
    }

    /**
     * 动态检查权限
     */
    private void checkSelfPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //动态判断手机储存空间的权限
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == -1 ||
                    ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE ) == -1 ||
                    ActivityCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) == -1){
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO},0x123);
            }else {
                record();
            }
        }else {
            record();
        }

    }

    @Click({R.id.imgBtn_pause,R.id.imgBtn_play})
    void onClick(View view){
        switch (view.getId()){
            case R.id.imgBtn_pause:
                if (imgBtn_pause.getText().toString().equals("停止")){
                    soundRecordUtil.stopRecord();
                    imgBtn_pause.setText("开始录制");
                }else {
                    checkSelfPermission();
                    imgBtn_pause.setText("停止");
                }
                break;
            case R.id.imgBtn_play:
                if (isFinish){
                    String url = path + File.separator + soundRecordUtil.getRecordFileName();
                    new PlayerUtil().playUrl(url);
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0x123){
            if (grantResults[0] == 0 && grantResults[1] == 0){
                record();
            }
        }
    }

    /**
     * 录音
     */
    private void record() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ){
            path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + getPackageName();
        }else {
            path = Environment.getDataDirectory().getAbsolutePath() + File.separator + getPackageName();
        }

        File file = new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        soundRecordUtil.setPathUserAudio(path);
        soundRecordUtil.setOnRecordChangeListener(new SoundRecordUtil.RecordChangeListener() {
            @Override
            public void onChange(int state, String fileName, double ema, long recordLength) {
                if (state == SoundRecordUtil.STATE_START_RECORD){
                    isFinish = false;
                }else if (state == SoundRecordUtil.STATE_STOP_RECORD){
                    isFinish = true;
                }
            }
        });
        soundRecordUtil.startRecord();
    }

}
